package meijuli.com.join51.business;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

/**
 *
 * @author jack
 */
public class TimerService extends Service {
    public static final String TAG = "TimerService";
    public static final int START = 1;
    public static final int STOP = 2;
    public static final int DESTROY = 3;
    private int serviceState = -1;
    private int totalTime = 120;
    private int remainTime = totalTime;
    private OnRemainTimeChangeListener onRemainTimeChangeListener;

    @Override
    public IBinder onBind(Intent intent) {
        return new TimerServiceBinder();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        serviceState = DESTROY;
        remainTime = -1;
        return super.onUnbind(intent);
    }

    public void startService() {
        if (serviceState == START) {
            return;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                serviceState = START;
                while (remainTime > 0 && serviceState != STOP) {
                    try {
                        Thread.sleep(1000);
                        remainTime--;
                        if (onRemainTimeChangeListener != null) {
                            onRemainTimeChangeListener.onRemainTimeChanged(remainTime);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                serviceState = STOP;
            }
        }).start();
    }

    public void setOnRemainTimeChangeListener(OnRemainTimeChangeListener onRemainTimeChangeListener) {
        this.onRemainTimeChangeListener = onRemainTimeChangeListener;
    }

    public interface OnRemainTimeChangeListener {
        void onRemainTimeChanged(int remainTime);
    }

    public class TimerServiceBinder extends Binder {

        /**
         * 获取定时器状态
         */
        public int getTimerServiceState() {
            return serviceState;
        }

        /**
         * 设置定时器的总时间
         */
        public void setTotalTime(int totalTime) {
            TimerService.this.totalTime = totalTime;
        }

        /**
         * 获取定时的剩余时间
         */
        public int getRemainTime() {
            return remainTime;
        }

        public void startTimerService() {
            startService();
        }

        public void resumeTimerService() {
            if (serviceState == START) {
                return;
            }
            serviceState = -1;
            remainTime = totalTime;
            startService();
        }

        public void stopTimerService() {
            serviceState = STOP;
            remainTime = -1;
        }

        public void destroyTimerService() {
            serviceState = DESTROY;
            remainTime = -1;
            TimerService.this.stopSelf();
        }

        public void setOnRemainTimeChangeListener(OnRemainTimeChangeListener onRemainTimeChangeListener) {
            TimerService.this.setOnRemainTimeChangeListener(onRemainTimeChangeListener);
        }
    }
}